<template>
    <div class="userinfo">
        <div class="userinfo-item">
            <img :src="form.avatar" alt="" class="userinfo-item-img" />
        </div>
        <div class="userinfo-item">
            <p class="userinfo-item-label">用户名</p>
            <el-input
                v-model="form.username"
                placeholder="请输入内容"
                disabled
            ></el-input>
        </div>
        <div class="userinfo-item">
            <p class="userinfo-item-label">密码</p>
            <el-input
                v-model="form.password"
                placeholder="请输入内容"
                type="password"
                disabled
            ></el-input>
        </div>
        <div class="userinfo-item">
            <p class="userinfo-item-label">年龄</p>
            <el-input
                v-model="form.age"
                placeholder="请输入内容"
                disabled
            ></el-input>
        </div>
        <div class="userinfo-item">
            <p class="userinfo-item-label">性别</p>
            <el-input
                v-model="form.sex"
                placeholder="请输入内容"
                disabled
            ></el-input>
        </div>
        <div class="userinfo-item">
            <p class="userinfo-item-label">电话</p>
            <el-input
                v-model="form.phone"
                placeholder="请输入内容"
                disabled
            ></el-input>
        </div>

        <div class="userinfo-item">
            <el-button @click="handleUserInfo">修改</el-button>
        </div>

        <el-dialog
            title="修改个人信息"
            :visible.sync="dialogVisible"
            width="30%"
        >
            <div class="userinfo-dialog-item">
                <p class="userinfo-item-label">头像</p>
                <el-upload
                    class="avatar-uploader"
                    action="url"
                    :on-change="handleChangeAvatar"
                    :show-file-list="false"
                    :auto-upload="false"
                >
                    <img :src="avatar" class="userUpdateImg" />
                </el-upload>
            </div>
            <div class="userinfo-dialog-item">
                <p class="userinfo-item-label">用户名</p>
                <el-input
                    v-model="form.username"
                    placeholder="请输入内容"
                    :disabled="isDisabled"
                ></el-input>
            </div>
            <div class="userinfo-dialog-item">
                <p class="userinfo-item-label">密码</p>
                <el-input
                    type="password"
                    v-model="form.password"
                    placeholder="请输入内容"
                    :disabled="isDisabled"
                ></el-input>
            </div>
            <div class="userinfo-dialog-item">
                <p class="userinfo-item-label">年龄</p>
                <el-input
                    v-model="form.age"
                    placeholder="请输入内容"
                    :disabled="isDisabled"
                ></el-input>
            </div>
            <div class="userinfo-dialog-item">
                <p class="userinfo-item-label">性别</p>
                <el-input
                    v-model="form.sex"
                    placeholder="请输入内容"
                    :disabled="isDisabled"
                ></el-input>
            </div>
            <div class="userinfo-dialog-item">
                <p class="userinfo-item-label">电话</p>
                <el-input
                    v-model="form.phone"
                    placeholder="请输入内容"
                    :disabled="isDisabled"
                ></el-input>
            </div>

            <div class="userinfo-dialog-item">
                <el-button @click="handleSubmit">提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getUserinfo, updateUserinfo } from "../api/index";
import { getUserAvatar } from "../utils.js";
import EventBus from "../common/EventBus";
import { httpOrHttps } from "../rules";

export default {
    name: "UserInfo",
    data() {
        return {
            form: {},
            dialogVisible: false,
            isDisabled: true,
            avatar: "",
        };
    },

    methods: {
        handleUserInfo() {
            this.dialogVisible = true;
            this.isDisabled = false;
            this.avatar = this.form.avatar;
        },

        async handleSubmit() {
            this.dialogVisible = false;
            let { username, age, sex, phone, password } = this.form;
            sex = sex === "男" ? 1 : 0;
            const res = await updateUserinfo("/user/modify", {
                username,
                password,
                age,
                sex,
                phone,
                avatar: httpOrHttps.test(this.avatar) ? null : this.avatar,
            });
            if (res.data.code === 0) {
                this.$message({
                    message: "用户信息修改成功",
                    type: "success",
                });
                this.getUserinfo();
                EventBus.emit("updateAvatar", getUserAvatar(username));
            } else {
                this.$message.error(res.data.msg);
            }
        },

        handleChangeAvatar(e) {
            var reader = new FileReader();
            reader.readAsDataURL(e.raw);
            reader.onload = (e) => {
                this.avatar = e.target.result;
            };
        },

        async getUserinfo() {
            try {
                const res = await getUserinfo("/user/get");
                const { username, sex, phone, age, password } = res.data.data;
                this.form = {
                    username,
                    sex: sex === 1 ? "男" : "女",
                    phone,
                    age,
                    password,
                };
                let userinfo = JSON.parse(localStorage.getItem("userinfo"));
                let avatar = getUserAvatar(userinfo.username);
                this.form = { ...this.form, avatar };
            } catch (err) {
                this.$message.error(err);
            }
        },
    },

    async mounted() {
        this.getUserinfo();
    },
};
</script>

<style lang="less">
.userinfo {
    &-item {
        margin-bottom: 30px;
        width: 500px;
        &-img {
            width: 150px;
            height: 150px;
        }
        &-label {
            margin: 0;
            margin-bottom: 10px;
            color: #aaa;
        }
    }

    &-dialog-item {
        width: 100%;
        margin-bottom: 30px;

        .userUpdateImg {
            width: 150px;
            height: 150px;
        }
    }
}
</style>